<?php



namespace Home\Model;

use Think\Model;



/**

 * 短信模型

 */

class SmsModel extends Model

{

	/**

	 * 添加一条短信记录

	 * $num 1是成功，2是失败

	 */

	public function addSms($data,$num){

		$res['sendee']  	= $data['mobile'];

		$res['content'] 	= $data['content'];

		$res['create_time'] = time();

		$res['status']      = $num;

		$this->add($res);

	}

	/**

	 * 退订的短信列表

	 * $id 退订的ID

	 * $cid 课程ID

	 */

	public function tuiding($id,$cid){

		//查找出老师的ID

		$uid = D('Home/Course') ->field('uid,title') ->where(array('id' => $cid)) -> find();

		//找出学生的手机号

		$stuMobile = D('Home/UserMember') ->field('mobile,nickname') ->where(array('id' =>$id)) ->find();

		//找出老师的手机号

		$mobile    = D('Home/UserMember') ->field('mobile,nickname') ->where(array('id' =>$uid['uid'])) ->find();

		 $arr = array(

		  	'sid'     => 0,

		  	'rid'      => $id,

		  	'content' => "您提交的退订已通过，未上部分的课程款将打回给您，请注意查收，谢谢您的支持",

		  	'type'    => 4,

		  	'time'    => time()

		  	);

		$brr = array(

		  	'sid'     => 0,

		  	'rid'      => $cid,

		  	'content' => "您的学生".$stuMobile['nickname']."已退订通过，平台将会把您已授课部分的课程款将打给您，谢谢您的支持。",

		  	'type'    => 4,

		  	'time'    => time()

		  	); 

		//发消息

		D('Message') ->add($arr);

		//给学生发短信

		D('Home/Message') ->sendSms($stuMobile['mobile'],"您已成功退订。");

		$con = "您的学生".$stuMobile['nickname']."已退订,详情请登录查看。";

		if(D('Home/Message') ->sendSms($mobile['mobile'],$con)){

			return true;

		}else{

			return false;

		}

	}

	/**
	 *
	 */

	public function daituidin($id,$cid){

		//查找出老师的ID

		$uid = D('Course') ->field('uid,title') ->where(array('id' => $cid)) -> find();

		//找出学生的手机号

		$stuMobile = D('UserMember') ->field('mobile,nickname') ->where(array('id' =>$id)) ->find();

		//找出老师的手机号

		$mobile    = D('UserMember') ->field('mobile') ->where(array('id' =>$uid['uid'])) ->find();

		$con = "您收到了".$stuMobile['nickname']."关于".$uid['title']."的退订,详情请登录查看。";

		if(D('Message') ->sendSms($mobile['mobile'],$con)){

			return true;

		}else{

			return false;

		}

	}

}